/*******************************************************************************************************
This dofile provides all the steps for the creation of the different varaibles needed for the analysis,
provides information about the design. It import all the data, and does all the merges needed. The data
are the same as those used in <<Favara, Giovanni, and Jean Imbs. 2015. "Credit supply and the price of
 housing.".
********************************************************************************************************/

/*********************************************************************************************************
									0. IMPORT THE DATA AND MERGE THEM
*********************************************************************************************************/

	use "$data/hmda.dta", clear
	merge 1:1 county year using "$data/hp_dereg_controls.dta", nogen keep(1 3)
	merge 1:1 county year using "$data/call.dta", nogen keep(1 3)

	*Weight variable
	sum w1
	replace w1=w1/r(mean)

	tsset county year
 	
/*********************************************************************************************************
										1. ASSESSING THE DESIGN
*********************************************************************************************************/

//1.a) Variables

*Ever change
	gen D_inter_bra=d.inter_bra
	bys state_n: egen ever_change=max(D_inter_bra)
	replace ever_change=(ever_change>0)

*First change
	sort county year
	gen first_change=(inter_bra>0&inter_bra[_n-1]==0&county==county[_n-1])

*Year first change
	gen year_first_change_temp=year*first_change
	bys state_n: egen year_first_change=max(year_first_change_temp)
	drop year_first_change_temp

*Years treated
	gen years_treated=max(year-year_first_change+1,0) if year_first_change!=0
	replace years_treated=0 if year_first_change==0
	tab years_treated, mis

*Years placebos
	gen years_placebo=min(year-year_first_change+1,0) if year_first_change!=0
	replace years_placebo=0 if year_first_change==0
	tab years_placebo, mis

	replace year_first_change=2006 if year_first_change==0
	tab year_first_change
